#include "questioncard.h"
#include "ui_questioncard.h"

QuestionCard::QuestionCard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::QuestionCard)
{
    ui->setupUi(this);
    init();
}

QuestionCard::~QuestionCard()
{
    delete ui;
}

QString getQuestion(const model::QuestionInfo& question, int limit = 20) {
    QString ret = question.content.mid(0, limit);
    if(question.content.size() > limit)
        ret.append(" . . .");
    return ret;
}

void QuestionCard::set(const model::QuestionInfo &question)
{
    ui->label_content->setText(getQuestion(question, 40));
}

void QuestionCard::set(const model::QuestionClusterInfo &questionCluster)
{
    if (questionCluster.questions.isEmpty())
        ui->label_content->setText("");
    else
        ui->label_content->setText(getQuestion(questionCluster.questions.front()));
    QString tags = questionCluster.tags.size() ? questionCluster.tags.front() : "";
    for(int i = 1, n = std::min(questionCluster.tags.size(), 3ll); i < n; ++i)
        tags.append(" ,  " + questionCluster.tags[i]);
    if(questionCluster.tags.size() > 3)
        tags.append(" ,  . . .");
    ui->label_tags->setText(tags);
    ui->label_times->setText(QString::number(questionCluster.cnt) + " 次");
}

void QuestionCard::init()
{
    initStyleSheet();
    initPanel();
}

void QuestionCard::initStyleSheet()
{
    this->setStyleSheet(R"(color: #333333;)");
    ui->panel->setStyleSheet(R"(#panel{border-radius: 12px; background-color: #eeeeee; border: 1px solid #DCDFE4;})");
}

void QuestionCard::initPanel()
{
    this->setFixedHeight(80);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    this->layout()->setContentsMargins(10, 0, 0, 0);
    this->layout()->setSpacing(0);

    ui->panel->layout()->setContentsMargins(20, 0, 20, 0);
    ui->panel->layout()->setSpacing(100);
}

#include <QMouseEvent>
void QuestionCard::mousePressEvent(QMouseEvent *event)
{
    event->accept();
}

void QuestionCard::mouseReleaseEvent(QMouseEvent *event)
{
    emit clicked();
    event->accept();
}

void QuestionCard::mouseMoveEvent(QMouseEvent *event)
{
    event->accept();
}
